package com.jh.user.business.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jh.user.business.UserRebateHistoryBusiness;
import com.jh.user.pojo.UserAccount;
import com.jh.user.pojo.UserRebateHistory;
import com.jh.user.repository.UserAccountRepository;
import com.jh.user.repository.UserRebateHistoryRepository;

@Service
public class UserRebateHistoryBusinessImpl implements UserRebateHistoryBusiness{

	
	@Autowired
	private UserRebateHistoryRepository  userRebateHistoryRepository;

	
	@Autowired
	private EntityManager em;
	
	@Autowired
	private UserAccountRepository     userAccountRepository;
	
	@Override
	public UserRebateHistory saveUserRebateHistory(
			UserRebateHistory userRebateHistory) {
		UserRebateHistory result = userRebateHistoryRepository.save(userRebateHistory);
		em.flush();
		return result;
	}

	
	@Override
	public Page<UserRebateHistory> queryUserRebateHistoryByUserid(long userid,
			Pageable pageAble) {
		return userRebateHistoryRepository.findRebateHistoryByUserid(userid, pageAble);
	}

	@Transactional
	@Override
	public UserAccount updateUserRebate(UserAccount userAccount,
			BigDecimal rebate, String orderType, String addorsub, String ordercode) {
		
		UserAccount olduserAccount = userAccountRepository.findUserAccountByUseridLock(userAccount.getUserId());
		
		BigDecimal curRebate = BigDecimal.ZERO;
		if(addorsub.equalsIgnoreCase("0")){
			if("0".equals(orderType.trim())) {
				curRebate = userAccount.getRebateBalance().add(rebate.setScale(2, BigDecimal.ROUND_HALF_UP));	
			}else {
				curRebate = userAccount.getRebateBalance().add(rebate.setScale(2, BigDecimal.ROUND_HALF_UP));
			}
					
			
		}else{
			
			curRebate = userAccount.getRebateBalance().subtract(rebate);		
			
		}
		
		
		olduserAccount.setRebateBalance(curRebate);
		UserAccount newAcct = userAccountRepository.save(olduserAccount);
		
		UserRebateHistory  rebateHistory = new  UserRebateHistory();
		rebateHistory.setAddOrSub(addorsub);
		rebateHistory.setRebate(rebate);
		rebateHistory.setCreateTime(new Date());
		rebateHistory.setCurRebate(curRebate);
		rebateHistory.setOrderType(orderType);
		rebateHistory.setOrderCode(ordercode);
		rebateHistory.setUserId(userAccount.getUserId());
		userRebateHistoryRepository.save(rebateHistory);

		
		
		em.flush();
		 
		return newAcct;
	}
	@Override
	public BigDecimal findsumRebateHistoryByUseridAnd( long userid,String[]  orderType, Date startTime,  Date endTime){
		
		BigDecimal sumRebate= userRebateHistoryRepository.findsumRebateHistoryByUseridAndTime(userid, orderType, startTime, endTime);
		
		return sumRebate;
		
	}


	@Override
	public List<Object[]> findSumRebateHistoryByUserIdBetweenWeek(long userId, String[] orderType, String startTime,String endTime) {
		return userRebateHistoryRepository.findSumRebateHistoryByUserIdBetweenWeek(userId,orderType,startTime);
	}


	@Override
	public List<Object[]> findSumByCreateDate(String startDate,String endDate,int limit1, int limit2,String brandId) {
		return userRebateHistoryRepository.findSumByCreateDate(startDate,endDate,limit1,limit2,brandId);
	}


	@Override
	public UserRebateHistory findUserRebateHistory(long userId, String orderType, BigDecimal rebate, String addOrSub,String ordercode) {
		List<UserRebateHistory> userRebateHistoryList=userRebateHistoryRepository.findUserRebateHistory(userId,orderType,rebate,addOrSub,ordercode);
		UserRebateHistory userRebateHistory=null;
		if(userRebateHistoryList!=null&&userRebateHistoryList.size()>0) {
			userRebateHistory=userRebateHistoryList.get(0);
		}
		return userRebateHistory;
	}
	
	
	
}
